﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlServerCe;
using System.Data.SqlClient;





namespace legends
{
    public partial class TileEditorForm : Form
    {
        private GameConstants gameConstants;

        
        //
        // Method fills dataGrid with "tiles" table from local database
        // http://www.dotnetperls.com/datagridview-tutorial
        //
        public void FillData()
        {
            // Open connection
            using (SqlCeConnection c = new SqlCeConnection(Properties.Settings.Default.Database1ConnectionString)) 
            {
                // Create new DataAdapter
                c.Open();
                
                using (SqlCeDataAdapter a = new SqlCeDataAdapter("SELECT * FROM Tiles", c))
                {
                    // Use DataAdapter to fill DataTable                
                    DataTable t = new DataTable();
                    
                    
                    
                    a.Fill(t);
                    DataRow myRow = t.NewRow();
                    Random RandomNumber = new Random();
                    int x = RandomNumber.Next(1000);
                    myRow[0] = x;
                    myRow[1] = "Shop";
                    t.Rows.Add(myRow);
                    
                    // Render data onto the screen
                    dataGridView1.DataSource = t;
                    
                }
            }
            
        }
        
        public TileEditorForm(GameConstants gameCon)
        {
            InitializeComponent();
            FillData();                         //fills tiles data to dataGrid
            gameConstants = gameCon;            //loading gameConstants from previous Form, needed for setting resolution     

            //
            // sizing and positioning of components
            // 
            this.ClientSize = new System.Drawing.Size(gameConstants.getResWidth(), gameConstants.getResHeight());

        }

        //
        // Opens ItemEditorForm, closes this form
        //
        private void ItemEditorButton_Click(object sender, EventArgs e)
        {
            ItemEditorForm itemEditorForm = new ItemEditorForm(gameConstants);

            itemEditorForm.Show();
            this.Visible = false;
        }

        //
        // Opens WorldEditorForm, closes this form
        //
        private void WorldEditorButton_Click(object sender, EventArgs e)
        {
            WorldEditForm worldEditorForm = new WorldEditForm(gameConstants);

            worldEditorForm.Show();
            this.Visible = false;
        }

        //
        // Closes application
        //
        private void ExitButton_Click(object sender, EventArgs e)
        {
            this.Dispose();
            Application.Exit();
        }

        private void UpdateButton_Click(object sender, EventArgs e)
        {
            // Open connection
            using (SqlCeConnection c = new SqlCeConnection(Properties.Settings.Default.Database1ConnectionString))
            {
                // Create new DataAdapter
                c.Open();

                using (SqlCeDataAdapter a = new SqlCeDataAdapter("SELECT * FROM Tiles", c))
                {
                    // Use DataAdapter to fill DataTable                
                    DataTable t = new DataTable();
                    a.Fill(t);
                    DataSet dbds = new Database1DataSet();
                    a.Update(t);
                    
                    

                    // Render data onto the screen
                    dataGridView1.DataSource = t;

                }
            }
        }
    }
}
